﻿var jsonUrl = "";
var actionUrl = "";
var actionTarget = "";
var ulID = "";
var jsonUrlParameter = "";
var actionUrlParameter = ""; 
$(function () {
	using('tree', function(){
		$(ulID).tree({checkbox:false, url:jsonUrl,cascadeCheck:false, onBeforeExpand:function (node) {
			var children = $(ulID).tree("getChildren", node.target);
				$(ulID).tree("options").url = jsonUrl +"?"+ jsonUrlParameter +"="+ node.id;
		}, onClick:function (node) {
			$(this).tree("toggle", node.target);
			if($(this).tree("isLeaf", node.target)){
				if (checkNull(actionUrl)){
					if (checkNull(actionTarget)){
						if (checkNull(actionUrlParameter)){
								window.open(actionUrl +"?temp="+new Date()+"&"+ actionUrlParameter+ "=" + node.id,actionTarget);
						}else {
								window.open(actionUrl,actionTarget);
						}
					}else {
						if (checkNull(actionUrlParameter)){
								window.open(actionUrl +"?"+ actionUrlParameter+ "=" + node.id);
						}else {
								window.open(actionUrl);
						}
					}
				}
			}else{
				window.open(actionUrl +"?isLeaf=1",actionTarget);
			}
		},
		onLoadSuccess:function(node, data){
			if(data!=''){
				var node=$(ulID).tree('getRoot');
				$(ulID).tree('expand',node.target);
		   }
		}
		});
	})
});



function tree(p_ulID,p_jsonUrl,p_jsonUrlParameter,p_actionUrl,p_actionUrlParameter,p_actionTarget){
	ulID = p_ulID;
	jsonUrl = p_jsonUrl;
	jsonUrlParameter = p_jsonUrlParameter;
	actionUrl = p_actionUrl;
	actionUrlParameter = p_actionUrlParameter;
	actionTarget = p_actionTarget;
}

function checkNull(val){
	if (val!=null&&val!=''&&val!='undefined'&&val!='null'){
		return true;
	}
	return false;
}


function nodeParameterResolve(parameter){
	var result = "";
	if (parameter.indexOf(',') != -1){
		var split1 = parameter.split(',');
		if (split1.length > 1){
			var tmp = "";
			for (var i = 1;i < split1.length;i++){
				if (split1[i].indexOf(':') != -1){
					tmp += '&' + split1[i].replaceAll(':','=');
				}
			}
			result = "?" + split1[0] + "" + tmp;
		}
	}else {
		result = "?" + parameter;
	}
	return result;
}

	
String.prototype.replaceAll  = function(oldStr,newStr){    
	return this.replace(new RegExp(oldStr,"gm"),newStr);    
}


